/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sirtet.tetris;

import java.util.List;
import java.util.Random;
import sirtet.Piece;
import sirtet.PieceProvider;
import sirtet.tetris.generator.TetrisPieceGenerator;

/**
 *
 * @author guangwei.zhu
 */
public class RandomPieceProvider extends PieceProvider {

    protected static final List[] pieceLists = new List[4];
    protected static double[] probability = new double[4];
    protected Random random;
    int initseed;

    public RandomPieceProvider() {
        initseed = new Random().nextInt();
        random = new Random(initseed);
    }

    public RandomPieceProvider(int seed) {
        initseed = seed;
        random = new Random(seed);
    }

    static {
        pieceLists[0] = TetrisPieceGenerator.enumerate(4);
        pieceLists[1] = TetrisPieceGenerator.enumerate(5);
        pieceLists[2] = TetrisPieceGenerator.enumerate(6);
        pieceLists[3] = TetrisPieceGenerator.enumerate(7);

  
        probability[0] = 0.82;
        probability[1] = 0.89;
        probability[2] = 0.95;
        probability[3] = 1;

/*
        probability[0] = 0.68;
        probability[1] = 0.80;
        probability[2] = 0.91;
        probability[3] = 1;
  */
    }

    public void reset() {
        random = new Random(initseed);
    }

    protected int choose(double x) {
        for (int i = 0; i < 4; i++) {
            if (x <= probability[i]) {
                return i;
            }
        }
        return 0;
    }

    public Piece requestPiece() {
        List selectedList = pieceLists[choose(random.nextDouble())];
        TetrisPiece selectedPiece = (TetrisPiece) selectedList.get(random.nextInt(selectedList.size()));

        // Must return a clone to avoid tampering the original copy
        return (Piece) selectedPiece.clone();
    }

    @Override
    public int remaining() {
        return Integer.MAX_VALUE;
    }
}
